package com.splant.android.smart.UILevel.Community.fragments;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.splant.android.R;

/**
 * Created by Administrator on 2017/6/21.
 */

public class Garden_Devices_Fragment extends Fragment {

    private FragmentManager fm;
    private RadioGroup rdoGp_changeDeviceList;
    private RadioButton rdoBtn_gatewayList;
    private RadioButton rdoBtn_waterList;
    private RadioButton rdoBtn_detectorList;
    private RadioButton rdoBtn_weatherStationList;
    private View mView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.garden_devices_fragment,null);

            rdoGp_changeDeviceList= (RadioGroup) mView.findViewById(R.id.rdoGp_changeDeviceList);
            rdoBtn_gatewayList= (RadioButton) mView.findViewById(R.id.rdoBtn_gatewayList);
            rdoBtn_waterList= (RadioButton) mView.findViewById(R.id.rdoBtn_waterList);
            rdoBtn_detectorList= (RadioButton) mView.findViewById(R.id.rdoBtn_detectorList);
            rdoBtn_weatherStationList= (RadioButton) mView.findViewById(R.id.rdoBtn_weatherStationList);

            fm = getFragmentManager();

            init();

            rdoGp_changeDeviceList.setOnCheckedChangeListener(menuChangeChick);
        }
        return mView;
    }

    private void init()
    {
        Fragment gatewayList= fm.findFragmentByTag("gatewayList");
        if (gatewayList==null)
        {
            FragmentTransaction ft = fm.beginTransaction();
            gatewayList=new Garden_GatewayList_Fragment();
            ft.add(R.id.fl_devices_container,gatewayList,"gatewayList");
            ft.commit();
        }
    }

    private RadioGroup.OnCheckedChangeListener menuChangeChick = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            FragmentTransaction ft = fm.beginTransaction();
            Fragment gatewayList = fm.findFragmentByTag("gatewayList");
            Fragment devices_waterList = fm.findFragmentByTag("devices_waterList");
            Fragment devices_detectorList = fm.findFragmentByTag("devices_detectorList");
            Fragment weatherStationList=fm.findFragmentByTag("weatherStationList");

            if (gatewayList!=null)
                ft.hide(gatewayList);

            if (devices_waterList!=null)
                ft.hide(devices_waterList);

            if (devices_detectorList!=null)
                ft.hide(devices_detectorList);

            if (weatherStationList!=null)
                ft.hide(weatherStationList);

            rdoBtn_gatewayList.setBackgroundResource(R.color.garden_color5);
            rdoBtn_waterList.setBackgroundResource(R.color.garden_color5);
            rdoBtn_detectorList.setBackgroundResource(R.color.garden_color5);
            rdoBtn_weatherStationList.setBackgroundResource(R.color.garden_color5);

            switch (checkedId)
            {
                case R.id.rdoBtn_gatewayList:
                    if (gatewayList==null)
                    {
                        gatewayList=new Garden_GatewayList_Fragment();
                        ft.add(R.id.fl_devices_container,gatewayList,"gatewayList");
                    }
                    else
                        ft.show(gatewayList);

                    rdoBtn_gatewayList.setBackgroundResource(R.color.garden_color4);
                    break;
                case R.id.rdoBtn_waterList:
                    if (devices_waterList==null)
                    {
                        devices_waterList=new Garden_WaterList_Devices_Fragment();
                        ft.add(R.id.fl_devices_container,devices_waterList,"devices_waterList");
                    }
                    else
                        ft.show(devices_waterList);

                    rdoBtn_waterList.setBackgroundResource(R.color.garden_color4);
                    break;
                case R.id.rdoBtn_detectorList:
                    if (devices_detectorList==null)
                    {
                        devices_detectorList=new Garden_DetectorList_Devices_Fragment();
                        ft.add(R.id.fl_devices_container,devices_detectorList,"devices_detectorList");
                    }
                    else
                        ft.show(devices_detectorList);

                    rdoBtn_detectorList.setBackgroundResource(R.color.garden_color4);
                    break;

                case R.id.rdoBtn_weatherStationList:
                    rdoBtn_weatherStationList.setBackgroundResource(R.color.garden_color4);
                    if (weatherStationList==null)
                    {
                        weatherStationList=new Garden_WeatherStationList_Fragment();
                        ft.add(R.id.fl_devices_container,weatherStationList,"weatherStationList");
                    }
                    else
                        ft.show(weatherStationList);
                    break;
            }
            ft.commit();


        }
    };
}
